package com.cby.library.common.ext

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.Fragment

/**
 * Activity 增加一个跳转的拓展函数
 * @receiver Activity
 * @param clazz Class<out Any>
 */
fun Fragment.start(clazz: Class<out Any>) {
    startActivity(Intent(activity, clazz))
}

/**
 * Activity 增加一个跳转的拓展函数,并且带参数[Bundle]
 * @receiver Activity
 * @param clazz Class<out Any>
 * @param bundle Bundle
 */
fun Fragment.start(clazz: Class<out Any>, bundle: Bundle) {
    startActivity(Intent(activity, clazz), bundle)
}

/**
 * Fragment 增加一个拓展函数,用来简化toast提示
 *
 * @receiver Activity
 * @param text String 提示文案
 */
fun Fragment.showToast(text: String) {
    Toast.makeText(activity!!.applicationContext, text, Toast.LENGTH_SHORT).show()
}

/**
 * Fragment 增加一个拓展函数,用来简化toast提示
 *
 * @receiver Activity
 * @param resId Int 提示文案资源Id
 */
fun Fragment.showToast(resId: Int) {
    Toast.makeText(activity!!.applicationContext, resId, Toast.LENGTH_SHORT).show()
}

/**
 * Fragment 增加一个拓展函数,用来获取Color的颜色值
 *
 * @receiver Activity
 * @param resId Int 提示文案资源Id
 * @return
 */
fun Fragment.getColor(resId: Int) : Int {
    return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        activity!!.getColor(resId)
    }else{
        activity!!.resources.getColor(resId)
    }
}

